// 题意：给定n(<=3000)个城市，m条边，现在有q(<=10000)条边有等可能的概率
//       增加为另一个费用，问最小生成树的期望。
//
// 题解：首先求一遍原图的最小生成树。然后对于这q条要变化的边，如果它不是
//       求得最小生成树上的边，那么它对应的最小生成树就是原图的。
//       现在难点就是如果变化的边在求得最小生成树上怎么办。
//       另外注意到这样的边只会有n-1条。
//       我们用dp[i][j]表示断掉(i, j)这条边连上两个树的最小费用。
//       可以这么维护，对每个点做为根dfs，dfs求的是根到子树的最小边，
//       沿途的时候对于每条边(u, v)用dfs当前的值更新dp[u][v]就行，可以
//       阅读下下面代码。复杂度O(n^2)。
//
// 统计：811ms
// run: $exec < input
#include <cstdio>
#include <algorithm>
#include <queue>

struct edge { int from, to, cost; };
bool operator<(edge const & a, edge const & b)
{
	return a.cost > b.cost;
}

int const inf = 1 << 30;
int const maxn = 3010;
int head[maxn], end_point[maxn * 2], cost[maxn * 2], next[maxn * 2];
int alloc;

int parent[maxn];
bool vis[maxn];
int map[maxn][maxn];
bool span_tree[maxn][maxn];
int dp[maxn][maxn];
int n, m, q;

void add_edge(int u, int v)
{
	end_point[alloc] = v; next[alloc] = head[u]; head[u] = alloc++;
	end_point[alloc] = u; next[alloc] = head[v]; head[v] = alloc++;
}

void init()
{
	alloc = 1;
	for (int i = 0; i < n; i++) {
		head[i] = 0;
		vis[i] = false;
		parent[i] = i;
		for (int j = 0; j < n; j++) span_tree[i][j] = false, dp[i][j] = map[i][j] = inf;
	}
}

int get_parent(int u)
{
	return u == parent[u] ? u : (parent[u] = get_parent(parent[u]));
}

void set_union(int u, int v)
{
	int tu = get_parent(u);
	int tv = get_parent(v);
	if (tu != tv) parent[tu] = tv;
}

int dfs(int root, int u, int parent)
{
	int ans = inf;
	for (int p = head[u]; p; p = next[p]) {
		int v = end_point[p];
		if (v == parent) continue;
		int tmp = dfs(root, v, u);
		ans = std::min(ans, tmp);
		if (tmp < dp[u][v]) dp[u][v] = dp[v][u] = tmp;
	}
	if (parent != root) ans = std::min(ans, map[root][u]);
	return ans;
}


const int BZ = 30 << 20;
char Buf[BZ + 1], *buf = Buf;

template <class T>
inline void scan(T &a) // method2: for huge input
{
	bool flag = false;
	for (a = 0; *buf < '0' || *buf > '9'; ++buf)
		if (*buf == '-') flag = true;
	for (; *buf >= '0' && *buf <= '9'; buf++)
		a = a * 10 + (*buf - '0');
	if (flag) a = -a;
}

int main()
{
	std::fread(Buf, 1, BZ, stdin); // method2

	for (; ; ) {
		scan(n); scan(m);
		if (!(n || m)) break;
		init();
		for (int i = 0, x, y, z; i < m; i++) {
			scan(x); scan(y); scan(z);
			map[x][y] = map[y][x] = z;
		}

		// prim
		int min_span = 0;
		vis[0] = true;
		std::priority_queue<edge> pq;
		for (int i = 1; i < n; i++) {
			if (map[0][i] >= inf) continue;
			edge tmp; tmp.from = 0; tmp.to = i; tmp.cost = map[0][i];
			pq.push(tmp);
		}
		for (int i = 1; i < n; i++) {
			while (!pq.empty() && vis[pq.top().to]) pq.pop();
			edge now = pq.top(); pq.pop();
			vis[now.to] = true;
			min_span += now.cost;
			span_tree[now.from][now.to] = span_tree[now.to][now.from] = true;
			add_edge(now.from, now.to);
			set_union(now.from, now.to);
			for (int j = 1; j < n; j++)
				if (!vis[j] && map[now.to][j] <= inf) {
					edge tmp;
					tmp.from = now.to; tmp.to = j; tmp.cost = map[now.to][j];
					pq.push(tmp);
				}
		}

		double ans = 0;
		scan(q);

		// main dfs
		for (int i = 0; i < n; i++) dfs(i, i, -1);

		for (int i = 0, x, y, z; i < q; i++) {
			scan(x); scan(y); scan(z);
			if (span_tree[x][y])
				ans += min_span - map[x][y] + std::min(z, dp[x][y]);
			else
				ans += min_span;
		}
		std::printf("%.4lf\n", ans / q);
	}
}

